﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;
using S9Engine;


public partial class Setting : Parser
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
        try
        {
            InitAll();
            
            if (!CheckLogin()) Response.Redirect("login.aspx");

            _parser.SetCurrentDir("~/html");
            _parser.SetLanguageFile("~/templates/language.txt", _currentLanguage);

            Session["CurrentPage"] = "Setting.aspx";

            string type = Request["type"] ?? "default";

            _DR.Refresh(System.Data.Objects.RefreshMode.StoreWins, _DR.Meta);

            LoadFile("setting.html", false);
            LoadSettings();
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
        finally
        {
            _parser.Parse("Main");
            _parser.Out("Main");
        }
    }

    private void LoadSettings()
    {
        string type = Request["type"] ?? "setting";
        string settingType = string.Empty;

        switch (type)
        {
            case "setting":
                {
                    _parser.Assign("pageIndex", "5");
                    _parser.Assign("settingTerm", _parser.GetLabelValue("Settings", _currentLanguage, LabelType.Lower));
                    settingType = "setting-" + _currentLanguage;
                    break;
                }
            case "support":
                {
                    _parser.Assign("pageIndex", "0");

                    _parser.Assign("settingTerm", _parser.GetLabelValue("OnlineSupport", _currentLanguage, LabelType.Lower));
                    settingType = type;
                    break;
                }
            default: return;
        }

        _parser.Assign("mode", type);

        List<Meta> metas = _DR.Meta.Where(m => m.Type.Equals(settingType)).ToList();

        foreach (Meta m in metas)
        {
            _parser.Assign("settingText", m.Key.Replace("_", " "));
            _parser.Assign("settingValue", m.Value);
            _parser.Assign("settingName", m.Key);
            _parser.Assign("settingID", m.ID.ToString());

            _parser.Parse("Main.Content.Setting.SettingItem");
        }

        if (metas.Count > 0) _parser.Parse("Main.Content.Setting");

        _parser.Parse("Main.Content");
    }
}